package sun.net.www.protocol.yml;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import sun.net.www.URLConnection;

/**
 * "yml" protocol {@link java.net.URLStreamHandler} impl.
 * <br> @author leo
 * <br> @date 2021/8/11
 * <br> @since 1.0.0
 */
public class YmlURLConnection extends URLConnection {


    /**
     * Create a URLConnection object.  These should not be created directly:
     * instead they should be created by protocol handers in response to
     * URL.openConnection.
     *
     * @param u The URL that this connects to.
     */
    public YmlURLConnection(URL u) {
        super(u);
    }

    @Override
    public void connect() throws IOException {

    }

    @Override
    public InputStream getInputStream() throws IOException {
        URL resource = getURL();
        String resourcePath = resource.getPath();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        while (resourcePath.startsWith("/")) {
            resourcePath = resourcePath.substring(1);
        }
        return classLoader.getResourceAsStream(resourcePath);
    }
}
